 /*
 * 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation
 * 存放如何更改状态
 * mutation 必须是同步函数
 *
 **/
import {
  TEST_API,
  GET_I9RIA,
  RECORD_I9RIA,
  ADD_COUNT,
  RECORD_USERINFO,
  RECORD_LOCATION,
  RECORD_SHARE_STATE,
  RECORD_INTEGRAL,
  RECORD_GOODINFO,
  OUT_LOGIN
} from './mutation-types.js'
import {cookie, removeCookie} from '../../config/utils'
// export const ADD_COUNT = 'ADD_COUNT'
// export const RECORD_USERINFO = 'RECORD_USERINFO'

export default {
  // 测试用
  [TEST_API] (state, type) {
    console.log(state)
    console.log(type)
  },
  [ADD_COUNT] (state, type) {
    type.amount = type.amount || 10
    state.count += type.amount
  },
  // 记录用户信息
  [RECORD_USERINFO] (state, info) {
    state.userInfo = info
    state.login = true
    let validity = 30
    cookie('USERID', info.openid, {expires: validity})
  },

  // 获取用户信息存入vuex
  [GET_I9RIA] (state, info) {
    if (state.I9RIA && info.wxJsApiConf) {
      return
    }
    let validity = 30
    cookie('unionid', info.unionid, {expires: validity})
    state.I9RIA = {...info}
    state.wxJsApiConf = info.wxJsApiConf
  },

  // 用户信息存入vuex
  [RECORD_I9RIA] (state, info) {
    if (state.I9RIA && info.wxJsApiConf) {
      return
    }
    let validity = 30
    cookie('unionid', info.unionid, {expires: validity})
    state.I9RIA = {...info}
    state.wxJsApiConf = info.wxJsApiConf
  },

  // 获取用户信息存入vuex
  [RECORD_LOCATION] (state, info) {
    state.location = info
  },

  // 代言人状态
  [RECORD_SHARE_STATE] (state, info) {
    state.shareState = info
  },

  // 临时记录商品信息
  [RECORD_GOODINFO] (state, info) {
    state.goodInfo = info
  },

  // 积分记录
  [RECORD_INTEGRAL] (state, info) {
    state.integralState = info
  },

  // 退出
  [OUT_LOGIN] (state) {
    state.userInfo = null
    state.login = false
    removeCookie('USERID')
    removeCookie('SID')
  }
}

